package ymos.fakelan;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Timer;
import java.util.TimerTask;

public class LanUdp {
	private static Timer timer;
	public static void LanSend(String motd,int port,int time) {
		String m = "[MOTD]"+motd+"[/MOTD]";
		String ad = "[AD]"+port+"[/AD]";
		byte[] data = (m+ad).getBytes();
		
		timer = new Timer();
		timer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				DatagramSocket socket=null;
				try {
					socket = new DatagramSocket();
					DatagramPacket packet = new DatagramPacket
					(data, data.length,InetAddress.getByName("255.255.255.255") , 4445);
					socket.send(packet);
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}finally {
					socket.close();
				}
			}
		}, time ,time);
	}
	
	public static void close() {
		if(timer!=null) {
			timer.cancel();
		}
	}
}
